//
//  GLGWLabel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/8.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "GLGWLabel.h"

@interface GLGWLabel()

@end

@implementation GLGWLabel

-(id)initWithInSuperView:(UIView *)aSuperView{
    if (self = [super init]) {
        self.text = @"";
        self.lineBreakMode = NSLineBreakByCharWrapping;
        [aSuperView addSubview:self];
    }
    return self;
}

-(id)initWithFont:(UIFont *)aFont theColor:(UIColor *)aColor inSuperView:(UIView *)aSuperView{
    if (self = [self initWithInSuperView:aSuperView]) {
        self.font = aFont;
        self.textColor = aColor;
    }
    return self;
}

-(id)initWithFrame:(CGRect)aFrame theFont:(UIFont *)aFont theColor:(UIColor *)aColor inSuperView:(UIView *)aSuperView{
    if (self = [self initWithFont:aFont theColor:aColor inSuperView:aSuperView]) {
        self.frame = aFrame;
    }
    return self;
}

#pragma mark -- 扩展
+(CGSize)sizeWithTheText:(NSString *)s_test theLabelWith:(CGFloat)with theTextFont:(UIFont *)fontSize{
    return [s_test boundingRectWithSize:CGSizeMake(with, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:fontSize} context:nil].size;
}

+(CGSize)sizeWithTheText:(NSString *)s_test theLabelHeight:(CGFloat)height theTextFont:(UIFont *)fontSize{
    return [s_test boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:fontSize} context:nil].size;
}

+(NSMutableAttributedString *)addImageName:(NSString *)iName theImageFram:(CGRect)aFram theTextString:(NSString *)tStr atImageIndex:(NSInteger)index{
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:tStr];
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    attch.image = [UIImage imageNamed:iName];
    attch.bounds = aFram;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:kHeightProportion_In_iPhoneTriploid(2)];
    [attri addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [tStr length])];
    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
    [attri insertAttributedString:string atIndex:index];
    return attri;
}

+(NSMutableAttributedString *)bllChangeStr:(NSString *)allStr changewithStr:(NSString *)lineSpace changeColoer:(UIColor *)wordColor withFont:(UIFont *)aFont{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:allStr];
    NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:lineSpace].location, [[noteStr string] rangeOfString:lineSpace].length);
    //需要设置的位置
    [noteStr addAttribute:NSForegroundColorAttributeName value:wordColor range:redRange];
    [noteStr addAttribute:NSFontAttributeName value:aFont range:redRange];
    return noteStr;
}

+ (NSMutableAttributedString *)changeLineWithSpace:(float)space theInoutText:(NSString *)aText{
    NSString *labelText = aText;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    return attributedString;
//    [self sizeToFit];
}

- (void)setNewEdgeInsets:(UIEdgeInsets)newEdgeInsets
{
    _newEdgeInsets = newEdgeInsets;
    [self textRectForBounds:self.bounds limitedToNumberOfLines:self.numberOfLines];
}

// 修改绘制文字的区域，edgeInsets增加bounds
//- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
//{
//    /* 调用父类该方法 注意传入的UIEdgeInsetsInsetRect(bounds, self.edgeInsets),bounds是真正的绘图区域 */
//    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.newEdgeInsets) limitedToNumberOfLines:numberOfLines];
//    rect.origin.x -= self.newEdgeInsets.left;
//    rect.origin.y -= self.newEdgeInsets.top;
//    rect.size.width += self.newEdgeInsets.left + self.newEdgeInsets.right;
//    rect.size.height += self.newEdgeInsets.top + self.newEdgeInsets.bottom;
//    return rect;
//}

- (void)drawTextInRect:(CGRect)rect
{
    //令绘制区域为原始区域，增加的内边距区域不绘制
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _newEdgeInsets)];
}

@end
